package models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.MyLogAction;

import models.DeveloperData;
import util.DBAction;

public class DeveloperData {
	private int developer_id;
	private String developername="";
	private String uname="";
	private String password="";
	private String email="";
	private String dev_design="";
	private int project_id=0;
	public int getDeveloper_id() {
		return developer_id;
	}

	public void setDeveloper_id(int developer_id) {
		this.developer_id = developer_id;
	}

	public String getDevelopername() {
		return developername;
	}

	public void setDevelopername(String developername) {
		this.developername = developername;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDev_design() {
		return dev_design;
	}

	public void setDev_design(String dev_design) {
		this.dev_design = dev_design;
	}

	public int getProject_id() {
		return project_id;
	}

	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}

	
	
	public int update() {
		String updateSQL = 
			    "update Developer "
			    + "set dev_name = '" + developername + "', dev_uname = '" + uname + 
				"', dev_pass = '" + password + "', dev_email = '" + email + 
				"', dev_design = '" + 
				dev_design  + "',project_id = " + project_id + 
				"where dev_id = " + developer_id;
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into Developer "
				+ "(dev_name, dev_uname, dev_pass, dev_email, dev_design, project_id) " + "values('" + developername
				+ "', '" + uname + "', '" + password + "', '" + email + 
				"', '" + dev_design + "', " + project_id +");";
		return DBAction.update(insertSQL);
	}
	public static ArrayList<DeveloperData> find(String selectionModifier) {
		ArrayList<DeveloperData> selection = new ArrayList<DeveloperData>();
		ResultSet resultSet = null;
		String query = "select * "+
				"from developer " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				DeveloperData Developer = new DeveloperData();
				Developer.developer_id=resultSet.getInt("dev_id");
				Developer.developername=resultSet.getString("dev_name");
				Developer.uname=resultSet.getString("dev_uname");
				Developer.password=resultSet.getString("dev_pass");
				Developer.email=resultSet.getString("dev_email");
				Developer.dev_design=resultSet.getString("dev_design");
				Developer.project_id=resultSet.getInt("project_id");
				selection.add(Developer);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("DeveloperData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return selection;
	}

	public static DeveloperData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * "+
				"from developer " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				DeveloperData Developer = new DeveloperData();
				Developer.developer_id=resultSet.getInt("dev_id");
				Developer.developername=resultSet.getString("dev_name");
				Developer.uname=resultSet.getString("dev_uname");
				Developer.password=resultSet.getString("dev_pass");
				Developer.email=resultSet.getString("dev_email");
				Developer.dev_design=resultSet.getString("dev_design");
				Developer.project_id=resultSet.getInt("project_id");
				DBAction.close(resultSet);
				DBAction.close(connection);
				return Developer;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("DeveloperData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}
	
	
}
